/*
 * Copyright (c) 1997-2012 InfoReach, Inc. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * InfoReach ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with InfoReach.
 *
 * CopyrightVersion 2.0
 *
 */

package com.inforeach.util.string;

/**
 * @author dmytros
 *
 */
public class StringTransformers
{
    public static IStringTransformer toStringTransformer(IStringBiTransformer bitransformer)
    {
        if (bitransformer instanceof IStringTransformer)
        {
            return (IStringTransformer) bitransformer;
        }
        else
        {
            return newStringTransformer(bitransformer);
        }
    }

    public static IStringTransformer newStringTransformer(IStringBiTransformer bitransformer)
    {
        return new AdapterToStringTransformer(bitransformer.forward(), bitransformer.reverse());
    }

    public static IStringTransformer newStringTransformer(IStringUniTransformer forward, IStringUniTransformer reverse)
    {
        return new AdapterToStringTransformer(forward, reverse);
    }

    public static IStringBiTransformer newStringBiTransformer(IStringUniTransformer forward, IStringUniTransformer reverse)
    {
        return new AdapterToStringTransformer(forward, reverse);
    }

    public static IStringBiTransformer toStringBiTransformer(IStringTransformer transformer)
    {
        if (transformer instanceof IStringBiTransformer)
        {
            return (IStringBiTransformer) transformer;
        }
        else
        {
            return newStringBiTransformer(transformer);
        }
    }

    public static IStringBiTransformer newStringBiTransformer(IStringTransformer transformer)
    {
        return new AdapterToStringTransformer(newStringUniTransformer(transformer, false), newStringUniTransformer(transformer, true));
    }

    public static IStringUniTransformer newStringUniTransformer(IStringTransformer transformer, boolean reverse)
    {
        return new AdapterToStringUniTransformer(transformer, reverse);
    }

    static class AdapterToStringTransformer implements IStringTransformer, IStringBiTransformer
    {
        final IStringUniTransformer forward_, reverse_;

        AdapterToStringTransformer(IStringUniTransformer forward, IStringUniTransformer reverse)
        {
            forward_ = forward;
            reverse_ = reverse;
        }

        public String forward(String str)
        {
            return transform(forward(), str);
        }

        public String reverse(String str)
        {
            return transform(reverse(), str);
        }

        public IStringUniTransformer forward()
        {
            return forward_;
        }

        public IStringUniTransformer reverse()
        {
            return reverse_;
        }
    }

    static class AdapterToStringUniTransformer implements IStringUniTransformer
    {
        final IStringTransformer transformer_;
        final boolean reverse_;

        AdapterToStringUniTransformer(IStringTransformer transformer, boolean reverse)
        {
            transformer_ = transformer;
            reverse_ = reverse;
        }

        public void transform(CharSequence source, StringBuilder target)
        {
            String str = source.toString();
            String res = reverse_ ? transformer_.reverse(str) : transformer_.forward(str);
            target.append(res);
        }
    }

    public static StringBuilder transform(IStringUniTransformer transformer, String str, StringBuilder builder)
    {
        transformer.transform(str, builder);
        return builder;
    }

    public static String transform(IStringUniTransformer transformer, String str)
    {
        return transform(transformer, str, new StringBuilder()).toString();
    }
}
